Ext.define('cloud.controller.Adpics', {
    extend: 'Ext.app.Controller',
    config: {
        refs: {
            homePic: 'homePic',
            adpicAdd: 'adpicAdd',
            adpic_add: 'homePic #adpic_add',
            adpic_search: 'homePic #adpic_search',
            adpic_sort: 'homePic #adpic_sort',
            adpic_submit: 'adpicAdd #adpic_submit',
            adpicEdit: 'adpicEdit',
            adpic_edit: 'adpicEdit #adpic_edit',
            adpic_delet: 'adpicEdit #adpic_delet',
            adpic_upload: 'adpicAdd #adpic_upload',
            adpic_showImage: 'adpicAdd #adpic_showImage'
        },
        control: {
            adpic_add: {
                tap: function () {
                    Ext.Viewport.mask({xtype: "loadmask"});
                    var adpic = this.getHomePic();
                    var adpicadd = this.getAdpicAdd() || Ext.create('cloud.view.homepic.AdpicAdd');
                    adpic.push(adpicadd);
                    Ext.Viewport.unmask();
                }
            },
            adpic_submit: {
                tap: function () {
                    var adpic_cadid = Ext.getCmp('adpic_cadid').getValue();
                    var adpic_name = Ext.getCmp('adpic_name').getValue();
                    var adpic_picUrl = Ext.getCmp('adpic_add_picUrl').getValue();
                    var adpic_explain = Ext.getCmp('adpic_explain').getValue();
                    var adpic_linkProduct = Ext.getCmp('adpic_linkProduct').getValue();
                    var adpic_display = Ext.getCmp('adpic_display').getValue();
                    if (Ext.isEmpty(adpic_name))
                        return Ext.Msg.alert('请输入广告名称');
                    if (Ext.isEmpty(adpic_explain))
                        return Ext.Msg.alert('请输入广告说明');
                    if (Ext.isEmpty(adpic_display))
                        return Ext.Msg.alert('请选择排序');
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    //局部变量this,在HTTP
                    var backthis = this;
                    var file = document.getElementById('adpic_img').files[0];
                    var paramsObj = {
                        cadid: adpic_cadid,
                        name: adpic_name,
                        picUrl: "",
                        explain: adpic_explain,
                        linkProduct: adpic_linkProduct,
                        display: adpic_display
                    }
                    this.addOrUpdateImgFn(file, paramsObj, "_add");
                }
            },

            'homePic list': {
                itemtap: function (list, index, element, record) {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    var adpicEdit = this.getAdpicEdit() || Ext.create('cloud.view.homepic.AdpicEdit', {title: record.get('name')});
                    var form = this.getHomePic().push(adpicEdit);
                    form.setRecord(record);
                    document.getElementById('imghead').src = record.get('picUrl')
                    Ext.Viewport.unmask();
                }
            },

            adpic_edit: {
                tap: function () {
                    var adpic_cadid = Ext.getCmp('adpic_cadid_edit').getValue();
                    var adpic_cdaid = Ext.getCmp('adpic_cdaid_edit').getValue();
                    var adpic_name = Ext.getCmp('adpic_name_edit').getValue();
                    var adpic_picUrl = Ext.getCmp('adpic_update_picUrl').getValue();
                    var adpic_explain = Ext.getCmp('adpic_explain_edit').getValue();
                    var adpic_linkProduct = Ext.getCmp('adpic_linkProduct_edit').getValue();
                    var adpic_display = Ext.getCmp('adpic_display_edit').getValue();
                    if (Ext.isEmpty(adpic_name))
                        return Ext.Msg.alert('请输入广告名称');
                    if (Ext.isEmpty(adpic_explain))
                        return Ext.Msg.alert('请输入广告说明');
                    if (Ext.isEmpty(adpic_display))
                        return Ext.Msg.alert('请选择排序');
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    var backthis = this;
                    var file = document.getElementById('adpic_img').files[0];
                    //没有改图片更新广告信息
                    if (file == null || file == undefined) {
                        //adpic_picUrl = Ext.getCmp('adpic_edit_picUrl').getValue(picUrl);
                        Ext.Ajax.request({
                            url: GLOBAL.api_url + 'adpic_update',
                            scope: this,
                            method: 'POST',
                            params: {
                                cadid: adpic_cadid,
                                name: adpic_name,
                                picUrl: '',
                                explain: adpic_explain,
                                linkProduct: adpic_linkProduct,
                                display: adpic_display
                            },
                            success: function (response) {
                                Ext.Viewport.unmask();
                                var returnText = Ext.decode(response.responseText);
                                if (returnText.Variables.Result.code == 0) {
                                    Ext.getStore('AdpicDistrictFindStore').load({params: {cadid: localStorage.getItem('cadid')}});
                                    var adpic = backthis.getHomePic();
                                    adpic.pop()
                                    Ext.Viewport.unmask();
                                }
                                Ext.Msg.alert(returnText.Variables.Message.messagestr);
                            },
                            failure: function () {//请求失败时执行操作
                                Ext.Viewport.unmask();
                                Ext.Msg.alert('请求失败', '服务器维护中....');
                            }
                        });
                    }
                    //修改图片
                    else {
                        var paramsObj = {
                            cdaid: adpic_cdaid,
                            cadid: adpic_cadid,
                            name: adpic_name,
                            picUrl: "",
                            explain: adpic_explain,
                            linkProduct: adpic_linkProduct,
                            display: adpic_display
                        }
                        this.addOrUpdateImgFn(file, paramsObj, "_update");
                    }
                }
            },

            adpic_delet: {
                tap: function () {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    this.getAdpicEdit().submit({
                        url: GLOBAL.api_url + 'adpic_delete',
                        scope: this,
                        method: 'POST',
                        success: function (from, result) {
                            Ext.Viewport.unmask();
                            if (result.Variables.Result.code == 0) {
                                this.getHomePic().pop()
                            }
                            Ext.getStore('AdpicDistrictFindStore').load({params: {cadid: localStorage.getItem('cadid')}});
                            Ext.Msg.alert(result.Variables.Message.messagestr);
                        },
                        failure: function () {
                            Ext.Viewport.unmask();
                            Ext.Msg.alert('请求失败', '服务器维护中....');
                        }
                    })
                }
            },

            adpic_search: {
                change: function () {
                    var Store = Ext.getStore('AdpicDistrictFindStore');
                    var value = this.getAdpic_search().getValue();
                    Store.clearFilter();
                    if (value != "") {
                        Store.filter('name', value, true);
                    }
                    Store.load({params: {cadid: localStorage.getItem('cadid')}});
                }
            },
            //首页广告的排序
            adpic_sort: {
                change: function (item, newValue, oldValue) {
                    var store = Ext.getStore('AdpicDistrictFindStore');
                    switch (newValue) {
                        case "adpicName_asc":
                            store.sort({
                                property: 'name',
                                direction: 'asc'
                            });
                            break;
                        case "adpicName_desc":
                            store.sort({
                                property: 'name',
                                direction: 'desc'
                            });
                            break;
                    }
                    store.load({params: {cadid: localStorage.getItem('cadid')}});
                }
            }
        }
    },
    /**
     * 上传图片(添加/修改首页图片)
     * @param file  获取文件
     * @param paramObj  所需参数对象
     * @param opt add Or update
     */
    addOrUpdateImgFn: function (file, paramObj, opt) {
        var backthis = this;
        var formdata = new FormData();
        formdata.append("Filedata", file);
        var http = new XMLHttpRequest();
        if (http.upload && http.upload.addEventListener) {
            // Response handler
            http.onreadystatechange = function (e) {
                if (this.readyState == 4) {
                    if (this.status == 200) {
                        var returnText = Ext.decode(this.responseText);
                        if (returnText.Variables.Result.code == 0) {
                            var picUrl = returnText.Variables.data.attachdir;
                            paramObj.picUrl = picUrl;
                            console.log(paramObj);
                            console.log(opt)
                            Ext.getCmp('adpic' + opt + '_picUrl').setValue(picUrl);
                            Ext.Ajax.request({
                                url: GLOBAL.api_url + 'adpic' + opt,
                                scope: this,
                                method: 'POST',
                                params: paramObj,
                                success: function (response) {
                                    Ext.Viewport.unmask();
                                    var returnText = Ext.decode(response.responseText);
                                    if (returnText.Variables.Result.code == 0) {
                                        Ext.getStore('AdpicDistrictFindStore').load({params: {cadid: localStorage.getItem('cadid')}});
                                        var adpic = backthis.getHomePic();
                                        adpic.pop()
                                        Ext.Viewport.unmask();
                                    }
                                    Ext.Msg.alert(returnText.Variables.Message.messagestr)
                                },
                                failure: function () {//请求失败时执行操作
                                    Ext.Viewport.unmask();
                                    Ext.Msg.alert('请求失败', '服务器维护中....');
                                }
                            });
                        }
                    }
                }
            };
            // Error handler
            http.upload.onerror = this.actionfailure;
        }
        // Send form with file using XMLHttpRequest POST request
        http.open('POST', GLOBAL.api_url + 'attachment_upload&updatatype=adpic');
        http.send(formdata);
    },
    /**
     * 请求失败时执行操作
     */
    actionfailure: function () {
        Ext.Viewport.unmask();
        Ext.Msg.alert('请求失败', '服务器维护中....');
    }
})